package portal.mybatis.dao.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import portal.mybatis.page.Page;
import beans.BaseResourceState;
import portal.mybatis.dao.mapper.ScheduleClusterMapper;

@Service
public class ClusterHostService {
	private static final String Page = null;
	@Autowired
	ScheduleClusterMapper scheduleClusterMapper;
	public boolean insertHostDetail(BaseResourceState state){
		if(state ==null)return false;
		return scheduleClusterMapper.insertSelective(state)>0;
	}
	public boolean isHostNameExist(String hostName){
		return scheduleClusterMapper.countByHostName(hostName)>0;
	}
	public List<Object> getClusterHosts(long currentPage, long pageSize){
		if(currentPage<1)
			currentPage=1;
		return selectPageHosts(currentPage*pageSize-pageSize,pageSize,null);
	}
	public Long countHosts(){
		return countPageHosts(null);
		//return scheduleClusterMapper.countHosts();
	}
	public Long countPageHosts(Page<BaseResourceState>page){
		return scheduleClusterMapper.countPageHosts(page);
	}
	public List<Object> selectPageHosts(long offset,long pageSize,String searchText){
		Page<BaseResourceState> page = new Page<BaseResourceState>(pageSize,offset,searchText);
		List<BaseResourceState> list = scheduleClusterMapper.selectPageHosts(page);
		return parseResourceState(list);
	}
	public List<Object>selectHisByHost(String hostName){
		BaseResourceState b=new BaseResourceState();
		b.setHostName(hostName);
		List<BaseResourceState> list = scheduleClusterMapper.selectHisByHost(b);
		return  parseResourceState(list);
	}
	private List<Object> parseResourceState(List<BaseResourceState>list){
		if(list==null ||list.size()==0)
			return null;
		List<Object> ret =new ArrayList<>();
		for(BaseResourceState e:list){
			if(e.getMem().contains("M")){
				e.setMem(e.getMem().replace("M", ""));
			}
			else if(e.getMem().contains("G")){
			int i = Integer.valueOf(e.getMem().replace("G", ""));
			e.setMem(i*1024+"");
		}else{}
			
			if(e.getSwp().contains("M")){
				e.setSwp(e.getSwp().replace("M", ""));
			}
			else if(e.getSwp().contains("G")){
			int i = Integer.valueOf(e.getSwp().replace("G", ""));
			e.setSwp(i*1024+"");
		}else{}
			
		//	e.setSwp(e.getSwp().replace("M", ""));
			e.setUt(e.getUt().replace("%", ""));
			ret.add(e);
		}
		return ret;
	}
}
